package by.yakusekmaxim.bookgid.webapp.page.home.book;

import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormChoiceComponentUpdatingBehavior;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.event.Broadcast;
import org.apache.wicket.event.IEvent;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RadioChoice;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;

import by.yakusekmaxim.bookgid.dataaccess.util.Filter;
import by.yakusekmaxim.bookgid.datamodel.Book;
import by.yakusekmaxim.bookgid.datamodel.Feedback;
import by.yakusekmaxim.bookgid.datamodel.UserAccount;
import by.yakusekmaxim.bookgid.datamodel.UserAccount2Book;
import by.yakusekmaxim.bookgid.datamodel.UserAccount2Book_;
import by.yakusekmaxim.bookgid.datamodel.enums.UserAccount2BookStatus;
import by.yakusekmaxim.bookgid.services.FeedbackService;
import by.yakusekmaxim.bookgid.services.UserAccount2BookService;
import by.yakusekmaxim.bookgid.webapp.app.BasicAuthenticationSession;

public class FeedbackAndReadPanel extends Panel {

	@Inject
	FeedbackService feedbackService;

	@Inject
	UserAccount2BookService userAccount2BookService;

	private UserAccount user = BasicAuthenticationSession.get().getUser();

	private Book book;

	private static final List<Long> SITES = Arrays.asList(new Long[] { 1l, 2l,
			3l, 4l, 5l, 6l, 7l, 8l, 9l, 10l });

	public FeedbackAndReadPanel(String id, final Book book) {
		super(id);
		this.book = book;

		UserAccount2Book userAccount2Book = userAccount2BookService
				.getUserAccount2BookDao(book, user);

		Feedback feedback = feedbackService.getBookUserEvaluation(book, user);
		if (feedback == null) {
			feedback = new Feedback();
			feedback.setUserAccount(user);
			feedback.setBook(book);
		}

		final Feedback feedbackF = feedback;

		final Label rating = new Label("ratingbook",
				feedbackService.getEvaluationBook(book));

		rating.setOutputMarkupId(true);
		add(rating);

		Form<Feedback> formFeedback = new Form<Feedback>("formFeedback",
				new CompoundPropertyModel<Feedback>(feedbackF));
		final RadioChoice<Long> radioChoice = new RadioChoice<Long>("rating",
				SITES, FeedbackRadioChoiceRendering.INSTANCE);
		AjaxFormChoiceComponentUpdatingBehavior radioChoiceAjixUpdade = new AjaxFormChoiceComponentUpdatingBehavior() {
			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				System.out.println(feedbackF.getRating());
				feedbackService.saveOrUpdate(feedbackF);
				rating.setDefaultModelObject(feedbackService
						.getEvaluationBook(book));
				target.add(rating);
			}
		};
		radioChoice.add(radioChoiceAjixUpdade);
		formFeedback.add(radioChoice);

		add(formFeedback);

		final Label readyRead = new Label("readyRead", getCountReadyRead());

		readyRead.setOutputMarkupId(true);
		add(readyRead);

		final Label iwanttoRead = new Label("iwanttoRead",
				getCountIwantToRead());

		iwanttoRead.setOutputMarkupId(true);
		add(iwanttoRead);

		final AjaxLink<Void> addReadyRead = new AjaxLink<Void>("addReadyRead") {
			@Override
			public void onClick(AjaxRequestTarget target) {
				UserAccount2Book userAccount2Book = userAccount2BookService
						.getUserAccount2BookDao(book, user);
				if (userAccount2Book == null) {
					userAccount2Book = new UserAccount2Book();
					userAccount2Book.setBook(book);
					userAccount2Book.setUserAccount(user);
				}
				userAccount2Book
						.setStatus(UserAccount2BookStatus.useraccountbookstatus_alreadyread);
				userAccount2BookService.saveOrUpdate(userAccount2Book);
				readyRead.setDefaultModelObject(getCountReadyRead());
				target.add(readyRead);
				iwanttoRead.setDefaultModelObject(getCountIwantToRead());
				target.add(iwanttoRead);

				this.add(AttributeModifier.replace("style", "display: none;"));
				target.add(this);

				EventRead eventRead = new EventRead();
				eventRead.setTarget(target);
				eventRead.setIwantToRead(AttributeModifier.replace("style",
						"display: block;"));
				eventRead.setDeleteIwantToRead(AttributeModifier.replace(
						"style", "display: block;"));
				send(getPage(), Broadcast.BREADTH, eventRead);
			}

			@Override
			public void onEvent(IEvent<?> event) {
				Object payload = event.getPayload();
				if (payload instanceof EventRead) {
					EventRead eventRead = (EventRead) payload;
					if (eventRead.getReadyRead() != null) {
						this.add(eventRead.getReadyRead());
						eventRead.getTarget().add(this);
					}
				}

			}
		};

		if (userAccount2Book != null) {
			if (userAccount2Book.getStatus() == UserAccount2BookStatus.useraccountbookstatus_alreadyread) {
				addReadyRead.add(AttributeModifier.replace("style",
						"display: none;"));
			}
		}
		addReadyRead.setOutputMarkupId(true);
		add(addReadyRead);

		AjaxLink<Void> addIwantToRead = new AjaxLink<Void>("addIwantToRead") {
			@Override
			public void onClick(AjaxRequestTarget target) {
				UserAccount2Book userAccount2Book = userAccount2BookService
						.getUserAccount2BookDao(book, user);
				if (userAccount2Book == null) {
					userAccount2Book = new UserAccount2Book();
					userAccount2Book.setBook(book);
					userAccount2Book.setUserAccount(user);
				}
				userAccount2Book
						.setStatus(UserAccount2BookStatus.useraccountbookstatus_iwanttoread);
				userAccount2BookService.saveOrUpdate(userAccount2Book);
				readyRead.setDefaultModelObject(getCountReadyRead());
				target.add(readyRead);
				iwanttoRead.setDefaultModelObject(getCountIwantToRead());
				target.add(iwanttoRead);

				this.add(AttributeModifier.replace("style", "display: none;"));
				target.add(this);

				EventRead eventRead = new EventRead();
				eventRead.setTarget(target);
				eventRead.setReadyRead(AttributeModifier.replace("style",
						"display: block;"));
				eventRead.setDeleteIwantToRead(AttributeModifier.replace(
						"style", "display: block;"));
				send(getPage(), Broadcast.BREADTH, eventRead);
			}

			@Override
			public void onEvent(IEvent<?> event) {
				Object payload = event.getPayload();
				if (payload instanceof EventRead) {
					EventRead eventRead = (EventRead) payload;
					if (eventRead.getIwantToRead() != null) {
						this.add(eventRead.getIwantToRead());
						eventRead.getTarget().add(this);
					}
				}
			}

		};

		if (userAccount2Book != null) {
			if (userAccount2Book.getStatus() == UserAccount2BookStatus.useraccountbookstatus_iwanttoread) {
				addIwantToRead.add(AttributeModifier.replace("style",
						"display: none;"));
			}
		}
		addIwantToRead.setOutputMarkupId(true);
		add(addIwantToRead);

		AjaxLink<Void> deleteIwantToRead = new AjaxLink<Void>(
				"deleteIwantToRead") {
			@Override
			public void onClick(AjaxRequestTarget target) {
				UserAccount2Book userAccount2Book = userAccount2BookService
						.getUserAccount2BookDao(book, user);
				if (userAccount2Book != null) {
					userAccount2BookService.delete(userAccount2Book);
				}
				readyRead.setDefaultModelObject(getCountReadyRead());
				target.add(readyRead);
				iwanttoRead.setDefaultModelObject(getCountIwantToRead());
				target.add(iwanttoRead);

				this.add(AttributeModifier.replace("style", "display: none;"));
				target.add(this);

				EventRead eventRead = new EventRead();
				eventRead.setTarget(target);
				eventRead.setReadyRead(AttributeModifier.replace("style",
						"display: block;"));
				eventRead.setIwantToRead(AttributeModifier.replace("style",
						"display: block;"));
				send(getPage(), Broadcast.BREADTH, eventRead);
			}

			@Override
			public void onEvent(IEvent<?> event) {
				Object payload = event.getPayload();
				if (payload instanceof EventRead) {
					EventRead eventRead = (EventRead) payload;
					if (eventRead.getDeleteIwantToRead() != null) {
						this.add(eventRead.getDeleteIwantToRead());
						eventRead.getTarget().add(this);
					}
				}
			}

		};

		if (userAccount2Book == null) {

			deleteIwantToRead.add(AttributeModifier.replace("style",
					"display: none;"));

		}
		deleteIwantToRead.setOutputMarkupId(true);
		add(deleteIwantToRead);

	}

	private Long getCountReadyRead() {
		Filter<UserAccount2Book> filter = new Filter<UserAccount2Book>();
		filter.setAttribute(UserAccount2Book_.status);
		filter.setValue(UserAccount2BookStatus.useraccountbookstatus_alreadyread);

		return userAccount2BookService.getCountByBook(filter, book);
	}

	private Long getCountIwantToRead() {
		Filter<UserAccount2Book> filter = new Filter<UserAccount2Book>();
		filter.setAttribute(UserAccount2Book_.status);
		filter.setValue(UserAccount2BookStatus.useraccountbookstatus_iwanttoread);

		return userAccount2BookService.getCountByBook(filter, book);
	}
}
